﻿component extends="ActiveDirectory" output="false"
{
    this.UserBadge = '\\scfappshare\ITS_Data\IDCentre\BadgePhotos\';
    
	public Users function init( )
	{
        return this;
	}
    
    public any function setADUser( required string ADUserAccount = "" )
    {        
        try
        {
            Rosetta = entityload('RosettaStone', {UserAccount = arguments.ADUserAccount});
            Badge = entityLoadByPK("Badges", Rosetta[1].getRosettaID());
            this.UserDN = this.SCFAD.getUserDNByAccountName(arguments.ADUserAccount);
            attrs = this.SCFAD.getUserAttributesByDN( trim(this.UserDN) );
            this.UserAttrs = {
                distinguishedName='#attrs.distinguishedName#',sAMAccountName='#attrs.sAMAccountName#',
                title='#attrs.title#',physicalDeliveryOfficeName='#attrs.physicalDeliveryOfficeName#',
                department='#attrs.department#',manager='#attrs.manager#', 
                displayName='#attrs.displayName#',userPrincipalName='#attrs.userPrincipalName#',
                memberOf='#attrs.memberOf#',givenName='#attrs.givenName#', 
                initials='#attrs.initials#', sn='#attrs.sn#',directReports='#attrs.directReports#',
                lastLogoff='#attrs.lastLogoff#',lastLogon='#attrs.lastLogon#',logonCount='#attrs.logonCount#',
                rosetta='#Rosetta[1].getRosettaID()#', Badge='#this.UserBadge#thumbs\#listgetat(Badge.getPhotoFileName(), 5, '\')#',
                UserDN = this.UserDN
            };            
        }
        catch(Any e)
        {
            this.UserDN = '[Not Found]';
            this.UserAttrs = {
                distinguishedName='[Not Found]',sAMAccountName='[Not Found]',
                title='[Not Found]',physicalDeliveryOfficeName='[Not Found]',department='[Not Found]',
                manager='[Not Found]', displayName='[Not Found]',userPrincipalName='[Not Found]',memberOf='[Not Found]',
                givenName='[Not Found]', initials='[Not Found]', sn='[Not Found]',directReports='[Not Found]',
                lastLogoff='[Not Found]',lastLogon='[Not Found]',logonCount='[Not Found]', rosetta='[Not Found]', Badge='[Not Found]',
                error = '#e#', UserDN = this.UserDN
            };
        }
        return this;
    }
    
    
    
    public any function getADUserManager(  )
    {        
        try
        {
            UserManagerDN = this.SCFAD.getUserAttributesByDN(this.UserDN,'manager').manager;
            attrs = this.SCFAD.getUserAttributesByDN( trim(UserManagerDN) );
            Rosetta = entityload('RosettaStone', {UserAccount = attrs.sAMAccountName});
            Badge = entityLoadByPK("Badges", Rosetta[1].getRosettaID());            
            this.ManagerAttrs = {
                distinguishedName='#attrs.distinguishedName#',sAMAccountName='#attrs.sAMAccountName#',
                title='#attrs.title#',physicalDeliveryOfficeName='#attrs.physicalDeliveryOfficeName#',
                department='#attrs.department#',manager='#attrs.manager#', 
                displayName='#attrs.displayName#',userPrincipalName='#attrs.userPrincipalName#',
                memberOf='#attrs.memberOf#',givenName='#attrs.givenName#', 
                initials='#attrs.initials#', sn='#attrs.sn#',directReports='#attrs.directReports#',
                lastLogoff='#attrs.lastLogoff#',lastLogon='#attrs.lastLogon#',logonCount='#attrs.logonCount#',
                rosetta='#Rosetta[1].getRosettaID()#', Badge='#this.UserBadge#thumbs\#listgetat(Badge.getPhotoFileName(), 5, '\')#'
            };            
        }
        catch(Any e)
        {
            this.ManagerAttrs = {
                distinguishedName='[Not Found]',sAMAccountName='[Not Found]',
                title='[Not Found]',physicalDeliveryOfficeName='[Not Found]',department='[Not Found]',
                manager='[Not Found]', displayName='[Not Found]',userPrincipalName='[Not Found]',memberOf='[Not Found]',
                givenName='[Not Found]', initials='[Not Found]', sn='[Not Found]',directReports='[Not Found]',
                lastLogoff='[Not Found]',lastLogon='[Not Found]',logonCount='[Not Found]', rosetta='[Not Found]', Badge='[Not Found]',
                error = '#e#'
            };
        }
        return this.ManagerAttrs;
    }
    
    public any function getSupervisorLatterUp( required string ADUserDN = "" )
    {  
        try
        {
            var Latter = ArrayNew(1);
            next_account = arguments.ADUserDN;
            previous_account = '';
            i = 1;            
            for (i=1; i LTE 50; i++)
            {
                Latter[i] = this.SCFAD.getUserAttributesByDN(next_account,'manager').manager;                
                if( next_account EQ Latter[i] ){
                    ArrayDeleteAt(Latter, i);
                    break;
                }else{
                    next_account = Latter[i];
                }
            }
        }
        catch(Any e)
        {
            var Latter = ArrayNew(1);
            Latter[1] = '#e#';
        }
        return Latter;
    }

    
    /*public any function getADUserDetails( required string UserAccount = -1 )
    {
        import models.*;
        RosettaStone = entityload("RosettaStone", {UserAccount = arguments.UserAccount});
        this.User.DN = this.SCFAD.getUserDNByAccountName(arguments.UserAccount);
        this.User.UserAccount = arguments.UserAccount;
        this.User.RosettaID = RosettaStone[1].getRosettaID();
        this.User.PersonIdNo = RosettaStone[1].getHRProd_PersonIDNo();
        this.User.WFC_PersonID = RosettaStone[1].getWFC_PersonID();
        this.User.SupervisorDN = this.SCFAD.getUserAttributesByDN(this.User.DN, "manager").manager;
        this.User.SupervisorUserAccount = this.SCFAD.getUserAttributesByDN(this.User.SupervisorDN, "sAMAccountName").sAMAccountName;
        RosettaStone = entityload("RosettaStone", {UserAccount = this.User.SupervisorUserAccount});
        this.User.SupervisorRosettaID = RosettaStone[1].getRosettaID();
        this.User.MemberOf = this.SCFAD.getUserAttributesByDN(this.User.DN, "memberOf").memberOf;
        this.User.MemberOf = Replace(ArrayToList(REMatch("\bCN=(.*?),", this.User.MemberOf )), ',,', ',', "all");
        return this;
    }*/
    
    
    public query function getADUserByDN( required string SearchString )
    {
        UserQuery = new query();
        UserQuery.setAttributes(GetAllUsers = getAllADUsers());
        UserQuery.addParam(name="username",value="%#Ucase( Left( arguments.SearchString,2) ) & Right( arguments.SearchString, len(arguments.SearchString)-2)#%",cfsqltype="VARCHAR"); 
        UserQuery.addParam(name="usernameLowerCase",value="%#Lcase(arguments.SearchString)#%",cfsqltype="VARCHAR");
        UserQuery.addParam(name="firstname",value="%#Lcase(arguments.SearchString)#%",cfsqltype="VARCHAR");
        UserQuery.addParam(name="lastaname",value="%#Lcase(arguments.SearchString)#%",cfsqltype="VARCHAR");
        UserQuery.addParam(name="displayName",value="%#arguments.SearchString#%",cfsqltype="VARCHAR"); 
        UserByDN = UserQuery.execute(
            sql="SELECT * FROM GetAllUsers WHERE sAMAccountName like :username OR 
                sAMAccountName like :usernameLowerCase OR 
                displayName LIKE :username OR 
                displayName LIKE :displayName OR
                sn LIKE :username OR
                sn LIKE :lastaname OR 
                givenName LIKE :firstname", 
            dbtype="query", maxRows="10");
        return UserByDN.getResult();        
    }
    
    
    public query function getUserOwnedGroups( required string UserDN )
    {
        var allgroups = this.SCFAD.getAllGroups();
        UserQuery = new query();
        UserQuery.setAttributes(allgroups = allgroups);
        UserOwnedGroups = UserQuery.execute(
            sql="SELECT * FROM allgroups WHERE managedBy = '#arguments.UserDN#'", 
            dbtype="query"
        );
        return UserOwnedGroups.getResult();
        
    }
    
    public any function getAllADUsers( )
    {
        return session.AllADUsers;
    }
    
    
    public query function getAllUserDepartments( )
    {
        UserQuery = new query();
        UserQuery.setAttributes(GetAllUsers = getAllADUsers());
        UserDepartment = UserQuery.execute(
            sql="SELECT Count( department ) AS DepartmentCount FROM GetAllUsers WHERE department <> ''  GROUP BY department ORDER BY DepartmentCount Desc", 
            dbtype="query"
        );
        return UserDepartment.getResult();
    }
    
    

}